package com.example.pro2;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
	ProgressBar pg_bar1,pg_bar2,pg_bar3,pg_bar4;
	CheckBox cb_1,cb_2,cb_3,cb_4;
	Button btn_load;
	int current1 = 0;
	int current2=0;
	int current3=0;
	int current4=0;
	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				pg_bar1.setProgress(current1++);
				if(current1==100){
					cb_1.setChecked(true);
				}
				break;
			case 2:
				pg_bar2.setProgress(current2++);
				if(current2==100){
					cb_2.setChecked(true);
				}
				break;
			case 3:
				pg_bar3.setProgress(current3++);
				if(current3==100){
					cb_3.setChecked(true);
				}
				break;
			case 4:
				pg_bar4.setProgress(current4++);
				if(current4==100){
					cb_4.setChecked(true);
				}
				break;
			default:
				break;
			}
		}
	};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }
	private void initEvent() {
		btn_load.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						while(current1<=pg_bar1.getMax()){
							try {
								Thread.sleep(100);
								handler.sendEmptyMessage(1);
							} catch (InterruptedException e) {
				
								e.printStackTrace();
							}
							
						}	
					}
				}).start();
				
				
				
new Thread(new Runnable() {
					
					@Override
					public void run() {
						while(current2<=pg_bar2.getMax()){
							try {
								Thread.sleep(90);
								handler.sendEmptyMessage(2);
							} catch (InterruptedException e) {
				
								e.printStackTrace();
							}
							
						}	
					}
				}).start();


new Thread(new Runnable() {
	
	@Override
	public void run() {
		while(current3<=pg_bar3.getMax()){
			try {
				Thread.sleep(80);
				handler.sendEmptyMessage(3);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
			
		}	
	}
}).start();
new Thread(new Runnable() {
	
	@Override
	public void run() {
		while(current4<=pg_bar4.getMax()){
			try {
				Thread.sleep(70);
				handler.sendEmptyMessage(4);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
			
		}	
	}
}).start();
			}
		});
		
		
		
		
	}
	private void initView() {
		pg_bar1=(ProgressBar) findViewById(R.id.pg_bar1);
		pg_bar2=(ProgressBar) findViewById(R.id.pg_bar2);
		pg_bar3=(ProgressBar) findViewById(R.id.pg_bar3);
		pg_bar4=(ProgressBar) findViewById(R.id.pg_bar4);
		
		cb_1=(CheckBox) findViewById(R.id.cb_1);
		cb_2=(CheckBox) findViewById(R.id.cb_2);
		cb_3=(CheckBox) findViewById(R.id.cb_3);
		cb_4=(CheckBox) findViewById(R.id.cb_4);
		
		btn_load=(Button) findViewById(R.id.btn_load);
		
		pg_bar1.setMax(100);
		pg_bar2.setMax(100);
		pg_bar3.setMax(100);
		pg_bar4.setMax(100);
	}
}
